package 蓝桥杯算法;

import java.util.Arrays;

public class day02 {
    public static void main(String[] args) {
        int[] arr={-5,-3,2,4,6};
        day02 a=new day02();
       int[] s1= a.sortedSquare(arr);
       int[] s2=a.sortedsquare2(arr);
       for(int i=0;i<s1.length;i++){
           System.out.println(s2[i]);
       }

    }
    public int[] sortedSquare(int[] a){//暴力排序
        int[] b=new int[a.length];
        for(int i=0;i<a.length;i++){
            b[i]=a[i]*a[i];
        }
        Arrays.sort(b);//直接排序
        return b;
    }
    public int[] sortedsquare2(int[] a){
        int []b=new int[a.length];
        int left=0;
        int right=a.length-1;
        int i=a.length-1;
        while(left<right){
            int result1=a[left]*a[left];
            int result2=a[right]*a[right];
            if(result1>result2){
                b[i]=result1;
                i--;
                left++;
            }
            else if(result1<result2){
                b[i]=result2;
                i--;
                right--;
            }
            else{
                b[i]=result1;
                i--;
                right--;
                left++;
            }
         }
        return b;
    }
  



}
